'''
Created on Apr 12, 2012

@author: Galvez, Martino, Ventura
'''
from pyevolve import G1DList

from pyevolve import GSimpleGA

from pyevolve import Mutators

# This function is the evaluation function, we want
# to give high score to more zero'ed chromosomes
def eval_func(chromosome):
    import sys
    sys.path.append("C:\\vs2010 workspace\\Projects\\Lib\\Lib\\Agente")
    sys.path.append("C:\\vs2010 workspace\\Projects\\Lib\\Lib\\Agente\\Framework")
    from Kosong import Kosong
    from _agents import RandomAgent, FileAgent
    from _base import run_match, match
    from Agente import MiniMaxObligatorio

    # Diccionario que contiene las diferentes fichas con sus valores, estos valores se iran ajustando
    parametros = {'0':0, 'a':chromosome[0], 'A':chromosome[0], 'b':chromosome[1], 'B':chromosome[1], 'c':chromosome[2], 'C':chromosome[2]}
        
    # Cantidad de partidas a jugar para obtener el muestreo
    cantMuestreo = 10
    # Variable que almacena la cantidad de partidas ganadas contra las perdidas
    score = 0

    for i in range(cantMuestreo):
        for move_number, moves, game_state in match(Kosong(), MiniMaxObligatorio(name='NuestroAgente', heuristic=MiniMaxObligatorio.__heuristic__, params=parametros), RandomAgent(name = 'Agente2')):
            if move_number is None:
                print 'Result: %s' % (moves)
                print 'Final board: %r'  % (game_state)             
                score += moves['NuestroAgente']

                print '==========Jugada' + str(i) +'============'
                print(str(fin-inicio))
                print 'Result: %s' % (moves)
                print 'Final board: %r'  % (printBoard(game_state.board))
                print 'Tiempo Total: ' + str((time()-inicio)/60) + ' min.'
                print '==========Jugada' + str(i) +'============'
    return score

# Genome instance, set the size of the list
genome = G1DList.G1DList(3)

# The evaluator function (objective function)
genome.evaluator.set(eval_func)
# THe possible values that each letter in the cromosome can take
genome.setParams(rangemin=0, rangemax=10)
# Change type of mutator
genome.mutator.set(Mutators.G1DListMutatorIntegerRange)

ga = GSimpleGA.GSimpleGA(genome)   
# Set number of generations
ga.setGenerations(10)
# Set size of the population
ga.setPopulationSize(10)
# Set mutation rate
ga.setMutationRate(0.05)

# Do the evolution, with stats dump
# frequency of 10 generations
ga.evolve(freq_stats=1)

# Best individual
resultados = ga.bestIndividual()
salida = ''
salida += 'a/A: ' 
salida += chr(resutados[0])
salida += '/n'
salida += 'b/B: ' 
salida += chr(resutados[1])
salida += '/n'
salida += 'c/C: ' 
salida += chr(resutados[2])
salida += '/n'
print salida
print '##################### FINAL #####################'

